package org.liang.auth.controller;

import org.liang.auth.dto.RoleDTO;
import org.liang.auth.service.IRoleService;
import org.liang.auth.domain.Role;
import org.liang.auth.query.RoleQuery;
import org.liang.base.utils.AjaxResult;
import org.liang.base.utils.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.List;

@RestController
@RequestMapping("/role")
public class RoleController {
    @Autowired
    public IRoleService roleService;


    /**
     * 保存和修改公用的
     * @param role  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult save(@RequestBody Role role){
        try {
            if( role.getId()!=null)
                roleService.update(role);
            else
                roleService.insert(role);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Serializable id){
        try {
            roleService.delete(id);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }


    /**
    * 删除所有对象信息
    * @param ids
    * @return
    */
    @PatchMapping
    public AjaxResult batchDelete(@RequestBody List<Serializable> ids){
        try {
            roleService.batchDelete(ids);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public AjaxResult selectById(@PathVariable("id")Serializable id)
    {
        try {
            Role role = roleService.selectById(id);
            return AjaxResult.success(role);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping
    public AjaxResult selectAll(){

        try {
            List< Role> list = roleService.selectAll();
            return AjaxResult.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    public AjaxResult queryPage(@RequestBody RoleQuery query)
    {
        try {
            PageInfo<Role> pageInfo = roleService.queryPage(query);
            return AjaxResult.success(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    /*
    * 授权操作
    * 前端传复杂数据时，自定义接收对象 DTO
    * 后端返回数据给前端，自定义接收对象 VO
    * */
    @PostMapping("/roleAuth")
    public AjaxResult roleAuth(@RequestBody RoleDTO roleDTO)
    {
        try {
            roleService.roleAuth(roleDTO);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("授权失败!" + e.getMessage());
        }
    }

    //做回显数据
    @PostMapping("/queryAuth/{id}")
    public AjaxResult queryAuth(@PathVariable("id") String id)
    {
        try {
            List<Serializable> ids = roleService.queryAuth(id);
            return AjaxResult.success(ids);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("授权失败!" + e.getMessage());
        }
    }
}
